﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MvcEngine.Core.Common.Slugs
{
    public class RussianSlugConverter : ISlugConverter
    {
        protected Dictionary<string, string> dic = new Dictionary<string, string>();
        protected string russianLetters = "А,Б,В,Г,Д,Е,Ё,Ж,З,И,Й,К,Л,М,Н,О,П,Р,С,Т,У,Ф,Х,Ц,Ч,Ш,Щ,Ъ,Ы,Ь,Э,Ю,Я,а,б,в,г,д,е,ё,ж,з,и,й,к,л,м,н,о,п,р,с,т,у,ф,х,ц,ч,ш,щ,ъ,ы,ь,э,ю,я, ";
        protected string englishLetters = "A,B,V,G,D,E,E,Zh,Z,I,J,K,L,M,N,O,P,R,S,T,U,F,H,C,Ch,Sh,Sch,,I,,E,U,Ya,a,b,v,g,d,e,e,zh,z,i,j,k,l,m,n,o,p,r,s,t,u,f,h,c,ch,sh,sch,,i,,e,u,ya,-";

        public RussianSlugConverter()
        {
            string[] russianChars = russianLetters.Split(',');
            string[] englishChars = englishLetters.Split(',');

            for (int i = 0; i < russianChars.Length; i++)
            {
                dic.Add(russianChars[i].ToString(), englishChars[i].ToString());
            }
        }

        #region ISlugConverter Members

        public virtual string Convert(string text)
        {
            string result = string.Empty;
            foreach (char c in text)
            {
                if (dic.ContainsKey(c.ToString()))
                    result += c.ToString().Replace(c.ToString(), dic[c.ToString()]);
                else
                    result += c.ToString();
            }
            return result;
        }

        #endregion
    }
}
